head和tail命令详解

您所在的位置:网站首页 parent company和head office区别 head和tail命令详解

head和tail命令详解

2023-08-21 21:47| 来源: 网络整理| 查看: 265

   

基础命令学习目录首页

 

原文链接:https://www.cnblogs.com/amosli/p/3496027.html

当要查看上千行的大文件时,我们可不会用cat命令把整个文件内容给打印出来,相反,我们可能只需要看文件的一小部分地内容(例如文件的前十行和后十行),我们也有可能需要打印出来前n行或后n行,也有可能打印除了前n行或后n行之外的所有行,也有可能需要实时监控log日志的更新,那么怎么实现呢?下面一起来看一下linux下使用率极高的head ,tail两个命令。

一、head命令详解

首先,输入head --help查看帮助信息:

复制代码 amosli@amosli-pc:~/learn/fd$ head --help Usage: head [OPTION]... [FILE]... Print the first 10 lines of each FILE to standard output. With more than one FILE, precede each with a header giving the file name. With no FILE, or when FILE is -, read standard input. Mandatory arguments to long options are mandatory for short options too. -c, --bytes=[-]K print the first K bytes of each file; with the leading `-', print all but the last K bytes of each file -n, --lines=[-]K print the first K lines instead of the first 10; with the leading `-', print all but the last K lines of each file -q, --quiet, --silent never print headers giving file names -v, --verbose always print headers giving file names --help display this help and exit --version output version information and exit K may have a multiplier suffix: b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024, GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y. 复制代码 head命令的语法格式为: head [OPTION]... [FILE]...

接下来将在实例中讲解各参数含义及用法:

实例: 1.使用head命令查看文件内容前十行

新建test.txt,共14行.

复制代码 amosli@amosli-pc:~/learn/fd$ cat -n test.txt 1 a 2 b 3 c 4 d 5 e 6 f 7 g 8 h 9 i 10 j 11 k 12 l 13 m 14 n 复制代码

使用head命令查看前十行,head命令默认显示文件前十行

复制代码 amosli@amosli-pc:~/learn/fd$ head test.txt a b c d e f g h i j 复制代码 2.-n参数,显示test.txt文件的前3行 amosli@amosli-pc:~/learn/fd$ head -n 3 test.txt a b c

英文提示信息:

-n, --lines=[-]K print the first K lines instead of the first 10;

 

3.-n参数显示除了文件最后3行外的所有内容 复制代码 amosli@amosli-pc:~/learn/fd$ head -n -3 test.txt a b c d e f g h i j k 复制代码

英文提示信息:

-n, --lines=[-]K print the first K lines instead of the first 10; with the leading `-', print all but the last K lines of each file

加上'-',打印所有内容除了最后的K行。

4.-c参数,按文件内容大小来打印,打印前2个字节的内容 amosli@amosli-pc:~/learn/fd$ head -c 2 test.txt a

2个字节就是一个“a”字母。

英文提示信息:

-c, --bytes=[-]K print the first K bytes of each file; 5.-c参数,打印除了最后2个字节的文件内容 复制代码 amosli@amosli-pc:~/learn/fd$ head -c -2 test.txt a b c d e f g h i j k l m 复制代码

英文提示信息:

-c, --bytes=[-]K print the first K bytes of each file; with the leading `-', print all but the last K bytes of each file 6.-q参数,打印时不显示文件名称 复制代码 amosli@amosli-pc:~/learn/fd$ head -q test.txt a b c d e f g h i j 复制代码

英文提示信息:

-q, --quiet, --silent never print headers giving file names

其实后面跟上--quiet,--silent都是一样的,都不会显示文件名称,和默认打印是一样的效果。

7.-v参数,打印是显示文件名称 复制代码 amosli@amosli-pc:~/learn/fd$ head -v test.txt ==> test.txt test.txt test.txt test2.txt test2.txt > test2.txt

这个时候你就可以看到前一个终端在里出现了‘xyz’

复制代码 amosli@amosli-pc:~/learn/fd$ tail -f test2.txt g h i j k l m n o p xyz 复制代码

这样就能实时监控项目里的log日志了。

如果想设定一个间隔时间去查看文件的更新应该怎么做呢?请看-s参数

5、-s参数 英文提示信息: -s, --sleep-interval=N with -f, sleep for approximately N seconds (default 1.0) between iterations. With inotify and --pid=P, check process P at least once every N seconds.

如每隔5秒查看一次test2.txt的内容是否更新

复制代码 amosli@amosli-pc:~/learn/fd$ tail -f -s 5 test2.txt j k l m n o p xyz 复制代码

 默认是1秒更新一次。

6.--pid参数

 tail 具有一个很意思的特性,当某个给定进程结束之后,tail也会随之终结.

假如我们正在读取一个不断增长的文件,进程Foo一直在向该文件追加数据,那么tail就会一直执行,直到进程Foo的结束.

$PID=$(pidof Foo) $tail -f file --pid $PID #当进程Foo结束之后,tail也会跟着结束

例如用gedit打开test2.txt,不断加入数据,然后在终端里使用tail进行监听,具体为:

复制代码 amosli@amosli-pc:~/learn/fd$ PID=$(pidof gedit) amosli@amosli-pc:~/learn/fd$ tail -f -s 2 test2.txt --pid $PID h i j k l m n o p xyz 复制代码

然后不断在gedit中追加入‘yyy’后保存,按理说终端里应该会更新,但我的终端不知为何没有更新数据,这里就不帖出来了。

关闭gedit后,tail监控也关闭掉了。

 

head和tail取文件第m行到第n行

[root@xiaoma /root/mcw] test!#vim test.txt [root@xiaoma /root/mcw] test!#cat test.txt1 ma2 chang3 wei4 mo5 jiang [root@xiaoma /root/mcw] test!#cat test.txt |head -4|tail -2 >test1.txt [root@xiaoma /root/mcw] test!#cat test1.txt3 wei4 mo

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3